package com.ea.eadp.nexus.test.pageDefinition;

import com.ea.eadp.nexus.test.common.PageOperation;
import com.ea.eadp.nexus.test.common.TestUtil;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

public class SigninPageWeb3 extends Signin_Web {
    private static final String showHidePasswordXpath = ".//*[@id='panel-login']/div[2]/div[3]/div[3]/a";

    private String uri;

    public SigninPageWeb3(WebDriver driver) {
        super(driver);
    }

    public void navigate(String uri) {
        this.uri = uri;
        navigate();
    }

    public String getUri() {
        return uri;
    }

    public void setUri(String uri) {
        this.uri = uri;
    }

    public void login() {
        log.info("Login");
        driver.findElement(By.cssSelector(".btn-login>span")).click();
    }
    public String getEmailError() {
        driver.switchTo().window(this.operationWindowHandle);
        return driver.findElement(By.cssSelector("div#email-status>div")).getText();
    }
    public String getPassWordError() {
        driver.switchTo().window(this.operationWindowHandle);
        return driver.findElement(By.cssSelector("div#password-status>div")).getText();
    }
    public String getTFACodeError() {
        driver.switchTo().window(this.operationWindowHandle);
        return driver.findElement(By.cssSelector("#twoFactorCode-status > div")).getText();
    }
    public String getrecaptchaError() {
        driver.switchTo().window(this.operationWindowHandle);
        return driver.findElement(By.cssSelector("div#recaptcha-status>div")).getText();
    }

    public void clickShowHidePassword() {
        driver.findElement(By.cssSelector(".show-pass>a")).click();
    }
    public void signUp() {
        log.info("Sign Up Page");
        driver.findElement(By.cssSelector(".create-link")).click();
    }
    public void setCaptcha(String value) {
        driver.switchTo().window(this.operationWindowHandle);
        log.info(value);
        PageOperation.SendKeyToElementAfterWait(driver, "recaptcha", value);
    }
    public void clickToResetPassword() throws Exception {
        driver.switchTo().window(this.operationWindowHandle);
        driver.findElement(By.cssSelector("#panel-login > div.portal-ux-element.checked > div > a.forgot-password-link")).click();
        PageOperation.WebDriverWaitByXpath(driver, "//div[@id='panel-reset']/div");
        if (!TestUtil.getCurrentUrl(driver).contains("/reset?")) {
            throw new Exception("validate goes to 'reset password' page.");
        }
    }

    public void setResetPasswordEmail(String email) throws Exception {
        driver.switchTo().window(this.operationWindowHandle);
        if (!TestUtil.getCurrentUrl(driver).contains("/reset")) {
            throw new Exception("setResetPasswordEmail must in page /resetPassword");
        }
        log.info(email);
        PageOperation.SendKeyToElementAfterWait(driver, "email", email);
    }

    public void clickToSubmitResetPassword() throws Exception {
        driver.switchTo().window(this.operationWindowHandle);
        driver.findElement(By.xpath("//*[@id='panel-reset']/div[4]/div/a/span")).click();
        PageOperation.WebDriverWaitByXpath(driver, "//div[@id='panel-reset-confirmation']");
    }
    public void responseToChallengeTFACode(String twoFactorCode, Boolean checkTrustThisDevice) {
        PageOperation.SendKeyToElement(driver, "twoFactorCode", twoFactorCode);
        By trustThisDevice = By.id("trustThisDevice");
        WebElement checkbox = driver.findElement(trustThisDevice);
        WebElement parent = (WebElement) ((JavascriptExecutor) driver).executeScript("return arguments[0].parentNode;", checkbox);
        if((checkTrustThisDevice && (!checkbox.isSelected()))
                || (!checkTrustThisDevice && (checkbox.isSelected()))){
            parent.click();
        }
        By btn_submit = By.cssSelector("div.portal-ux-button > a.btn-tfa-login");
        driver.findElement(btn_submit).click();
    }

}
